package model;

import java.util.Collection;
import java.util.TreeSet;

public class Memory {

	// Singelton!
	private static final Memory memory = new Memory();

	private static int width;
	private static int opcodeWidth;
	private static MinCommandSet minCommandSet;

	private static boolean isSetup = false;

	public int getOpcodeWidth() {
		return opcodeWidth;
	}

	private Memory() {

	}

	public static void setup(MinCommandSet minCommandSet, int width,
			int opcodeWidth) {
		Memory.minCommandSet = minCommandSet;
		Memory.width = width;
		Memory.opcodeWidth = opcodeWidth;
		Memory.isSetup = true;
	}

	public static int getOpcodeForCommand(Class<? extends Command> cmd) {
		for (Integer key : Memory.minCommandSet.getCommands().keySet()) {
			if (Memory.minCommandSet.getCommands().get(key).getClass().equals(
					cmd)) {
				return key;
			}
		}
		return 0;
	}

	public static Memory getMemory() {
		if (!isSetup) {
			throw new IllegalStateException("Memory needs to be set up!");
		}
		return memory;
	}

	public int getWidth() {
		return width;
	}

	public Pointer pointer = new Pointer();

	public Collection<Register> register = new TreeSet<Register>();

	public int getPointer() {
		return pointer.getIndex();
	}

	public void setPointer(int pointer) {
		this.pointer.setIndex(pointer);

	}

	public Collection<Register> getRegister() {
		return register;
	}

	public void setRegister(Collection<Register> register) {
		this.register = register;
	}

	public static MinCommandSet getMinCommandSet() {
		return minCommandSet;
	}

}
